package com.hnx.admin.aurora.system.controller;

import com.hnx.admin.aurora.annotation.Log;
import com.hnx.admin.aurora.enums.BusinessType;
import com.hnx.admin.aurora.system.domain.param.role.form.RoleDelForm;
import com.hnx.admin.aurora.system.domain.param.role.form.RoleForm;
import com.hnx.admin.aurora.system.domain.param.role.query.RoleQuery;
import com.hnx.admin.aurora.system.domain.param.role.query.UserRoleQuery;
import com.hnx.admin.aurora.system.domain.param.user.form.RoleStateForm;
import com.hnx.admin.aurora.system.domain.param.user.form.SysUserRoleForm;
import com.hnx.admin.aurora.system.domain.param.user.form.UsersRoleForm;
import com.hnx.admin.aurora.system.service.DeptService;
import com.hnx.admin.aurora.system.service.RoleService;
import com.hnx.admin.aurora.system.service.UserService;
import com.hnx.admin.aurora.utils.UUIDUtil;
import com.hnx.admin.aurora.web.json.ResultJson;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/*
创建人： 倔强的头发
创建时间：2025/6/20
功能描述:

*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/role")
public class SysRoleController {
    private final RoleService roleService;
    private final DeptService deptService;
    private final UserService userService;
    @PreAuthorize("@auth.hasPermi('system:role:list')")
    @GetMapping("/list")
    public ResultJson list(RoleQuery role)
    {
        return roleService.selectRoleList(role);
    }
    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@auth.hasPermi('system:role:query')")
    @GetMapping(value = "/{roleId}")
    public ResultJson getInfo(@PathVariable String roleId)
    {
        return roleService.selectRoleById(roleId);
    }
    @PreAuthorize("@auth.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultJson edit(@Valid @RequestBody RoleForm role)
    {
        return roleService.updateRole(role);
    }
    /**
     * 新增角色
     */
    @PreAuthorize("@auth.hasPermi('system:role:add')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultJson add(@Validated @RequestBody RoleForm role)
    {
        role.setRoleId(UUIDUtil.getUUID());
        return roleService.insertRole(role);
    }
    /**
     * 删除角色
     */
    @PreAuthorize("@auth.hasPermi('system:role:remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public ResultJson remove(@PathVariable String[] roleIds)
    {
        RoleDelForm roleDelForm = new RoleDelForm();
        roleDelForm.setRoleIds(roleIds);
        return roleService.deleteRoleByIds(roleDelForm);
    }
    /**
     * 获取对应角色部门树列表
     */
    @PreAuthorize("@auth.hasPermi('system:role:query')")
    @GetMapping(value = "/deptTree/{roleId}")
    public ResultJson deptTree(@PathVariable("roleId") String roleId)
    {
        return deptService.selectDeptTreeList(roleId);
    }
    /**
     * 修改保存数据权限
     */
    @PreAuthorize("@auth.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dataScope")
    public ResultJson dataScope(@RequestBody RoleForm role)
    {
        return roleService.authDataScope(role);
    }
    /**
     * 查询已分配用户角色列表
     */
    @PreAuthorize("@auth.hasPermi('system:role:list')")
    @GetMapping("/authUser/allocatedList")
    public ResultJson allocatedList(UserRoleQuery user)
    {
        return userService.selectAllocatedList(user);
    }
    /**
     * 查询未分配用户角色列表
     */
    @PreAuthorize("@auth.hasPermi('system:role:list')")
    @GetMapping("/authUser/unallocatedList")
    public ResultJson unallocatedList(UserRoleQuery user)
    {
        return userService.selectUnallocatedList(user);
    }
    /**
     * 批量选择用户授权
     */
    @PreAuthorize("@auth.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public ResultJson selectAuthUserAll(@Valid UsersRoleForm usersRoleForm)
    {
        return roleService.insertAuthUsers(usersRoleForm);
    }
    /**
     * 批量取消授权用户
     */
    @PreAuthorize("@auth.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public ResultJson cancelAuthUserAll(@Valid UsersRoleForm usersRoleForm)
    {
        return roleService.deleteAuthUsers(usersRoleForm);
    }
    /**
     * 取消授权用户
     */
    @PreAuthorize("@auth.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public ResultJson cancelAuthUser(@RequestBody @Valid SysUserRoleForm userRole)
    {
        UsersRoleForm usersRoleForm = new UsersRoleForm();
        usersRoleForm.setRoleId(userRole.getRoleId());
        usersRoleForm.setUserIds(new String[]{userRole.getUserId()});
        return roleService.deleteAuthUsers(usersRoleForm);
    }
    /**
     * 状态修改
     */
    @PreAuthorize("@auth.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public ResultJson changeStatus(@RequestBody @Valid RoleStateForm role)
    {
        return roleService.updateRoleStatus(role);
    }
}
